﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VK.Comm.Session;
using VK.Comm.Cookie;

namespace VK.Casemeet.Merchants
{
    public static class AdminBLL
    {
        public static bool IsAdmin()
        {
            AdminEntity admin = SessionOperate<AdminEntity>.GetEntity(CS.ShopAdminSessionKey);
            if (admin == null)
            {
                admin = CookieOperate<AdminEntity>.GetEntity(CS.ShopAdminCookieKey);
            }
            if (admin != null)
            {
                SessionOperate<AdminEntity>.SetEntity(CS.ShopAdminSessionKey, admin);
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int Enter(string email, string password, bool isSave)
        {
            int result = 0;
            AdminEntity admin = null;
            int num = AdminDAL.Enter(email, password);
            switch (num)
            {
                case 1: 
                    admin = AdminDAL.GetAdminByAccount(email); 
                    result = 1;
                    SessionOperate<AdminEntity>.SetEntity(CS.ShopAdminSessionKey, admin);
                    if (isSave)
                    {
                        CookieOperate<AdminEntity>.SetEntity(CS.ShopAdminCookieKey, admin, 30);
                    }
                    break;
                default:
                    result = num;
                    break;
            }
            return result;
        }

        public static void Leave()
        {
            SessionOperate<AdminEntity>.RemoveEntity(CS.ShopAdminSessionKey);
            CookieOperate<AdminEntity>.RemoveEntity(CS.ShopAdminCookieKey);
        }

        public static AdminEntity GetSessionAdmin()
        {
            AdminEntity admin = SessionOperate<AdminEntity>.GetEntity(CS.ShopAdminSessionKey);
            if (admin == null)
            {
                admin = CookieOperate<AdminEntity>.GetEntity(CS.ShopAdminCookieKey);
                if (admin == null)
                {
                    System.Web.HttpContext.Current.Response.Redirect("/ShopAdmin/Default.aspx");
                }
                else
                {
                    SessionOperate<AdminEntity>.SetEntity(CS.ShopAdminSessionKey, admin);
                }
            }
            return admin;
        }

        public static int SetAdminPassword(int adminID, string oldPassword, string newPassword)
        {
            return AdminDAL.SetAdminPassword(adminID, oldPassword, newPassword);
        }

        public static int ResetPass(int adminID, string newpass)
        {
            return AdminDAL.ResetPass(adminID, newpass);
        }

        public static int FindPass(string email, string newpass)
        {
            return AdminDAL.FindPass(email, newpass);
        }

        public static int Register(ShopEntity shop)
        {
            int result = 0;

            if (AdminDAL.GetAdminByAccount(shop.Account) == null)
            {
                result = ShopDAL.InsertShop(shop);
                if (result > 0)
                {
                    AdminEntity admin = AdminDAL.GetAdminByAccount(shop.Account);
                    SessionOperate<AdminEntity>.SetEntity(CS.ShopAdminSessionKey, admin);
                    result = 1;
                }
            }
            else
            {
                result = 3;
            }

            return result;
        }
    }
}
